home *** CD-ROM | disk | FTP | other *** search
/ BCI NET / BCI NET Dec 94.iso / archives / programming / source / fbm12s.lha / fbpalet.c < prev    next >
C/C++ Source or Header  |  1994-07-18  |  6KB  |  207 lines

  1. /*****************************************************************
  2.  * fbpalet.c: FBM Release 1.2 07-Apr-93 Michael Mauldin
  3.  *
  4.  * Copyright (C) 1989-1993 by Michael Mauldin.  Permission is granted
  5.  * to use this file in whole or in part for any purpose, educational,
  6.  * recreational or commercial, provided that this copyright notice
  7.  * is retained unchanged.  This software is available to all free of
  8.  * charge by anonymous FTP and in the UUNET archives.
  9.  *
  10.  * fbpalet.c: Replace one image's color map with another image's.
  11.  *          The color index plane is not modifed by this action.
  12.  *
  13.  *          Includes option for removing duplicates.  That is
  14.  *          Suppose both color 1 and color 127 are both <0,0,0>
  15.  *          (pure black)...with the -d option, all uses of color
  16.  *          127 get converted to color 1.  The colormap is not
  17.  *          modified by this action.
  18.  *
  19.  * USAGE
  20.  *     % fbpalet < image1 > image2
  21.  *
  22.  * EDITLOG
  23.  *     LastEditDate = Mon Jun 25 00:03:55 1990 - Michael Mauldin
  24.  *     LastFileName = /usr2/mlm/src/misc/fbm/fbpalet.c
  25.  *
  26.  * HISTORY
  27.  * 07-Apr-93  Michael Mauldin (mlm) at Carnegie-Mellon University
  28.  *    Added -J switch
  29.  *
  30.  * 25-Jun-90  Michael Mauldin (mlm@cs.cmu.edu) Carnegie Mellon
  31.  *    Package for Release 1.0
  32.  *
  33.  * 01-May-90  Michael Mauldin (mlm) at Carnegie-Mellon University
  34.  *     Created.
  35.  *****************************************************************/
  36.  
  37. # include <stdio.h>
  38. # include <math.h>
  39. # include "fbm.h"
  40.  
  41. # define USAGE \
  42.   "Usage: fbpalet [ -m<mapname ] [ -d ] [ -<type> ] < image > image"
  43.  
  44. #ifndef lint
  45. static char *fbmid =
  46. "$FBM fbpalet.c <1.2> 07-Apr-93 (C) 1989-1993 by Michael Mauldin, source \
  47. code available free from MLM@CS.CMU.EDU and from UUNET archives$";
  48. #endif
  49.  
  50. int debug = 0;
  51. int removedup = 0;
  52. double expon = 1.0;
  53.  
  54. main (argc, argv)
  55. char *argv[];
  56. { FBM image;
  57.   FBM mapimage;
  58.   int outtype = DEF_8BIT;
  59.   char *fname = NULL;
  60.   char *mapname = NULL;
  61.  
  62.   /* Get the options */
  63.   while (--argc > 0 && (*++argv)[0] == '-')
  64.   { while (*++(*argv))
  65.     { switch (**argv)
  66.       {
  67.     case 'D':    debug++; break;
  68.     case 'd':    removedup++; break;
  69.         case 'm':    mapname = *argv+1; SKIPARG; break;
  70.     case 'A':    outtype = FMT_ATK; break;
  71.     case 'B':    outtype = FMT_FACE; break;
  72.     case 'F':    outtype = FMT_FBM; break;
  73.     case 'G':    outtype = FMT_GIF; break;
  74.     case 'I':    outtype = FMT_IFF; break;
  75.     case 'J':    outtype = FMT_JPEG; break;
  76.     case 'L':    outtype = FMT_LEAF; break;
  77.     case 'M':    outtype = FMT_MCP; break;
  78.     case 'P':    outtype = FMT_PBM; break;
  79.     case 'S':    outtype = FMT_SUN; break;
  80.     case 'T':    outtype = FMT_TIFF; break;
  81.     case 'X':    outtype = FMT_X11; break;
  82.     case 'Z':    outtype = FMT_PCX; break;
  83.     default:        fprintf (stderr, "%s\n", USAGE);
  84.                         exit (1);
  85.       }
  86.     }
  87.   }
  88.  
  89.   /* Clear the memory pointers so alloc_fbm won't be confused */
  90.   image.cm  = image.bm  = (unsigned char *) NULL;
  91.   mapimage.cm = mapimage.bm = (unsigned char *) NULL;
  92.  
  93.   /* Read the image */
  94.   if (read_bitmap (&image, fname))
  95.   { 
  96.     if (image.hdr.planes == 1 && image.hdr.clrlen == 0)
  97.     { fprintf (stderr, "fbpalet only works on color images\n");
  98.     }
  99.  
  100.     /* Read the mapimage */
  101.     if (mapname && read_bitmap (&mapimage, mapname))
  102.     { 
  103.       if (mapimage.hdr.planes == 1 && mapimage.hdr.clrlen == 0)
  104.       { fprintf (stderr, "fbpalet only works on mapped color images\n");
  105.         exit (0);
  106.       }
  107.  
  108.       if (dup_clr (&mapimage, &image) &&
  109.           write_bitmap (&image, stdout, outtype))
  110.       { exit (0); }
  111.     }
  112.  
  113.     /* Still call dup_clr to transform duplicate colors */
  114.     else if (mapname == NULL)
  115.     { if (dup_clr (NULL, &image) &&
  116.           write_bitmap (&image, stdout, outtype))
  117.       { exit (0); }
  118.     }
  119.     
  120.   }
  121.  
  122.   exit (1);
  123. }
  124.  
  125. /****************************************************************
  126.  * dup_clr: Copy colormap from input to output
  127.  *        Duplicates are removed, even if input image is NULL.
  128.  ****************************************************************/
  129.  
  130. dup_clr (input, output)
  131. FBM *input, *output;
  132. { register int i, j, clrlen;
  133.   register unsigned char *ic, *oc;
  134.   unsigned char *r, *g, *b;
  135.   unsigned char cmap[256];
  136.   int changes = 0, colors;
  137.  
  138.   /*-------- First copy over the new colormap --------*/
  139.   if (input)
  140.   { clrlen = input->hdr.clrlen;
  141.  
  142.     /* If output image has another color map, free the old one */
  143.     if (output->hdr.clrlen != input->hdr.clrlen)
  144.     { if (output->hdr.clrlen > input->hdr.clrlen)
  145.       { fprintf (stderr,
  146.         "fbpalet: warning, new color map is smaller (%d vs %d)\n",
  147.         input->hdr.clrlen, output->hdr.clrlen);
  148.       }
  149.  
  150.       if (output->hdr.clrlen > 0) free (output->cm);
  151.       output->cm = (unsigned char *) malloc (input->hdr.clrlen);
  152.     }
  153.  
  154.     /* Replace colormap */
  155.     output->hdr.clrlen = clrlen;
  156.     ic = input->cm;
  157.     oc = output->cm;
  158.   
  159.     for (i=0; i < clrlen; i++)
  160.     { *oc++ = *ic++; }
  161.   }
  162.  
  163.   /*-------- Now map duplicate colors to lowest index --------*/
  164.  
  165.   if (!removedup) return (1);
  166.  
  167.   clrlen = output->hdr.clrlen;
  168.   colors = clrlen / 3;
  169.  
  170.   /* CMAP will compute transform of body of image */
  171.   for (i=0; i<colors; i++) { cmap[i] = i; }
  172.  
  173.   r = output->cm;
  174.   g = r + colors;
  175.   b = g + colors;
  176.  
  177.   for (i=1; i<colors; i++)
  178.   { for (j=0; j<i; j++)
  179.     { if (r[i] == r[j] && g[i] == g[j] && b[i] == b[j])
  180.       { cmap[i] = j;
  181.  
  182.     if (debug)
  183.     { fprintf (stderr, "Dup: %3d -> %3d <%3d,%3d,%3d>\n",
  184.            i, j, r[i], g[i], b[i]);
  185.     }
  186.  
  187.         changes++;
  188.     break;
  189.       }
  190.     }
  191.   }
  192.  
  193.   if (changes)
  194.   { fprintf (stderr, "fbpalet: %d duplicated colormap entries\n", changes); }
  195.  
  196.   /* Change indices of duplicated colors */
  197.   if (changes)
  198.   { ic = output->bm;
  199.     oc = ic + output->hdr.plnlen * output->hdr.planes;
  200.  
  201.     for (; ic<oc; ic++)
  202.     { *ic = cmap[*ic]; }
  203.   }
  204.  
  205.   return (1);
  206. }
  207.